#include "GroupList.h"
#include "ui_GroupList.h"
#include "ConfigManager.h"

#include <QMessageBox>
#include <QDir>
#include <QDebug>
#include <QFileInfoList>
#include <QFileInfo>

GroupList::GroupList(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::GroupList)
{
    m_ui->setupUi(this);
	Update();
	hide();
}

GroupList::~GroupList()
{
    delete m_ui;
}

void GroupList::Update()
{
	m_ui->listWidget->clear();
	QFileInfoList list;
	if (CONFIG_MANAGER->GetSavedGroupList(list))
	{
		for (int i = 0; i < list.count(); i++)
			m_ui->listWidget->addItem(list.at(i).fileName());
	}
}

void GroupList::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void GroupList::on_button_apply_clicked()
{
	int curIdx = m_ui->listWidget->currentRow();
	if (curIdx > -1)
	{
		emit sigGroupSelected(m_ui->listWidget->currentItem()->text());
		hide();
	}
	else
		QMessageBox::warning(this, "warning", "group is not selected");
}

void GroupList::on_button_delete_clicked()
{
	int curIdx = m_ui->listWidget->currentRow();
	if (curIdx > -1)
	{
		QString gName = m_ui->listWidget->takeItem(curIdx)->text();
		QDir dir("../conf/group/");
		if (!dir.remove(gName))
			qDebug() << "GroupList::on_button_delete_clicked() : gName is not exist";
	}
	else
		QMessageBox::warning(this, "warning", "group is not selected");
}

void GroupList::on_button_close_clicked()
{
	hide();
}
